package client.gui.lobby;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

import lib.Commands;
import lib.Config;

public class JoinPanel extends JPanel implements SelectorUser {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// @jve:decl-index=0:visual-constraint="5,9"

	private JLabel jLabel = null;
	public JComboBox jComboBox = null;
	private JRadioButton jRadioButton = null;
	private JRadioButton jRadioButton1 = null;
	private ButtonGroup buttonGroup = null;
	private JButton jButton = null;
	private JLabel jLabel1 = null;
	private JPanel jPanel = null;
	private Lobby parent = null;
	private mapSelector selector = null;

	public JTextArea jTextArea = null;
	public String[] UniversesInfo = new String[10];
	int Team = 1;
	private JPanel jPanel1 = null;

	private JLabel jLabel2 = null;

	private JButton CreatenewVerse = null;

	public JProgressBar jProgressBar = null;
	public java.awt.event.ActionListener comboboxListerner = new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent e) {
			jTextArea.setText(UniversesInfo[jComboBox.getSelectedIndex()]);
		}
	};

	private JButton refresh = null;

	public JoinPanel(Lobby l) {
		parent = l;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */

	private void initialize() {
		GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
		gridBagConstraints31.gridx = 121;
		gridBagConstraints31.gridy = 24;
		GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
		gridBagConstraints12.gridx = 120;
		gridBagConstraints12.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints12.gridy = 27;
		GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
		gridBagConstraints21.gridx = 121;
		gridBagConstraints21.gridy = 25;
		GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
		gridBagConstraints11.fill = GridBagConstraints.BOTH;
		gridBagConstraints11.gridy = 25;
		gridBagConstraints11.weightx = 1.0;
		gridBagConstraints11.weighty = 1.0;
		gridBagConstraints11.gridx = 120;
		GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
		gridBagConstraints6.gridx = 120;
		gridBagConstraints6.anchor = GridBagConstraints.WEST;
		gridBagConstraints6.gridy = 24;
		GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
		gridBagConstraints5.gridx = 0;
		gridBagConstraints5.gridy = 24;
		jLabel1 = new JLabel();
		jLabel1.setText("Team:");
		jLabel1.setPreferredSize(new Dimension(120, 24));
		GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
		gridBagConstraints4.gridx = 121;
		gridBagConstraints4.anchor = GridBagConstraints.CENTER;
		gridBagConstraints4.fill = GridBagConstraints.NONE;
		gridBagConstraints4.insets = new Insets(30, 0, 0, 30);
		gridBagConstraints4.ipadx = 0;
		gridBagConstraints4.weightx = 0.0;
		gridBagConstraints4.gridwidth = 1;
		gridBagConstraints4.gridy = 27;
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.fill = GridBagConstraints.VERTICAL;
		gridBagConstraints1.gridy = 23;
		gridBagConstraints1.weightx = 1.0;
		gridBagConstraints1.anchor = GridBagConstraints.WEST;
		gridBagConstraints1.gridx = 120;
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridheight = 24;
		gridBagConstraints.gridwidth = 120;
		gridBagConstraints.anchor = GridBagConstraints.WEST;
		gridBagConstraints.gridy = 0;
		jLabel = new JLabel();
		jLabel.setText("Universum:");
		jLabel.setPreferredSize(new Dimension(100, 24));
		this.setLayout(new GridBagLayout());
		this.setVisible(false);
		this.setSize(new Dimension(562, 354));
		this.add(jLabel, gridBagConstraints);
		this.add(getJComboBox(), gridBagConstraints1);
		this.add(getJPanel(), gridBagConstraints6);
		this.add(getJButton(), gridBagConstraints4);
		this.add(jLabel1, gridBagConstraints5);
		this.add(getJTextArea(), gridBagConstraints11);

		this.add(getJPanel1(), gridBagConstraints21);
		this.add(getJProgressBar(), gridBagConstraints12);
		this.add(getRefresh(), gridBagConstraints31);
		selector = new mapSelector(this);
	}

	/**
	 * This method initializes jComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.setPreferredSize(new Dimension(120, 24));
			jComboBox.setSelectedIndex(-1);
			jComboBox.addActionListener(comboboxListerner);
		}
		return jComboBox;
	}

	/**
	 * This method initializes jRadioButton
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getJRadioButton() {
		if (jRadioButton == null) {
			jRadioButton = new JRadioButton();
			jRadioButton.setText("2");
			jRadioButton.setBackground(Color.blue);
		}
		return jRadioButton;
	}

	/**
	 * This method initializes jRadioButton1
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getJRadioButton1() {
		if (jRadioButton1 == null) {
			jRadioButton1 = new JRadioButton();
			jRadioButton1.setText("1");
			jRadioButton1.setSelected(true);
			jRadioButton1.setBackground(Color.red);
		}
		return jRadioButton1;
	}

	public ButtonGroup getButtonGroup() {
		buttonGroup = new ButtonGroup();
		buttonGroup.add(jRadioButton);
		buttonGroup.add(jRadioButton1);
		return buttonGroup;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Join");
			jButton.setHorizontalAlignment(SwingConstants.LEFT);
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (jRadioButton1.isSelected())
						Team = 1;
					else
						Team = 2;
					System.out.println("Team: " + Team);
					parent.send(Commands.JOIN,
							(jComboBox.getSelectedIndex() + 1) + " " + Team);
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.anchor = GridBagConstraints.WEST;
			gridBagConstraints2.gridy = -1;
			gridBagConstraints2.gridx = -1;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.gridy = -1;
			gridBagConstraints3.gridx = -1;
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(getJRadioButton1(), gridBagConstraints2);
			jPanel.add(getJRadioButton(), gridBagConstraints3);
			this.getButtonGroup();
		}
		return jPanel;
	}

	/**
	 * This method initializes jTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.setEditable(false);
			jTextArea.setPreferredSize(new Dimension(300, 200));
			jTextArea.setSize(new Dimension(400, 300));
		}
		return jTextArea;
	}

	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setColumns(1);
			gridLayout.setHgap(0);
			gridLayout.setVgap(2);
			gridLayout.setRows(3);
			jLabel2 = new JLabel();
			jLabel2.setText(Config.mapName);
			jPanel1 = new JPanel();
			jPanel1.setLayout(gridLayout);
			jPanel1.add(jLabel2, null);
			jPanel1.add(getCreatenewVerse(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes CreatenewVerse
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCreatenewVerse() {
		if (CreatenewVerse == null) {
			CreatenewVerse = new JButton();
			CreatenewVerse.setText("Create new Verse");
			CreatenewVerse
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							selector.setVisible(true);
							jLabel2.setText(Config.mapName);
						}
					});
		}
		return CreatenewVerse;
	}


	public void setMap(String mapName) {
		parent.send(Commands.JOIN, "new " + Config.mapName);
		parent.send(Commands.JOIN,"");
		jLabel2.setText(mapName);
		jComboBox.setSelectedIndex(jComboBox.getItemCount()-1);
	}

	/**
	 * This method initializes jProgressBar
	 * 
	 * @return javax.swing.JProgressBar
	 */
	private JProgressBar getJProgressBar() {
		if (jProgressBar == null) {
			jProgressBar = new JProgressBar();
			jProgressBar.setPreferredSize(new Dimension(200, 14));
		}
		return jProgressBar;
	}

	/**
	 * This method initializes refresh	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getRefresh() {
		if (refresh == null) {
			refresh = new JButton();
			refresh.setText("Refresh");
			refresh.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					parent.send(Commands.JOIN,"");
				}
			});
		}
		return refresh;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
